/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import business.EventManager;
import business.SearchingManager;
import entity.Event;
import entity.Participant;
import entity.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.inject.Named;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Alessandro
 */
@ManagedBean
@SessionScoped
public class EventBean implements Serializable{
    
    private Event event;
    
    private Event eventViewed;
    
    private long eventViewedId;
    
    private int oldInvited;

    private User selectedInvited;
    
    private List<String> invited = new ArrayList<>();
    
    @EJB
    private EventManager em;
    

    /**
     * Creates a new instance of EventBean
     */
    public EventBean() {
    }

    
    public Event getEvent() {
        if(event == null){
            event=new Event();
        }
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public EventManager getEm() {
        return em;
    }

    public void setEm(EventManager em) {
        this.em = em;
    }

    public User getSelectedInvited() {
        return selectedInvited;
    }

    public void setSelectedInvited(User selectedInvited) {
        this.selectedInvited = selectedInvited;
    }

    public List<String> getInvited() {
        return invited;
    }

    public void setInvited(List<String> invited) {
        this.invited = invited;
    }

    public Event getEventViewed() {
        return eventViewed;
    }

    public void setEventViewed(Event eventViewed) {
        this.eventViewed = eventViewed;
    }

    public long getEventViewedId() {
        return eventViewedId;
    }

    public void setEventViewedId(long eventViewedId) {
        this.eventViewedId = eventViewedId;
    }
    
    public int getOldInvited() {
        return oldInvited;
    }

    public void setOldInvited(int oldInvited) {
        this.oldInvited = oldInvited;
    }
    
    
    
    public String save(){
        System.out.println(this.getInvited().toString());
        em.save(event, invited);
        System.out.println(event.getTitle());
        event = null;
        invited = null;
        return "home";
    }
    
    public void invite(){
        System.out.println("chiamto invited"+ selectedInvited.getEmail());
        this.invited.add(selectedInvited.getEmail());
        System.out.println(invited.toString());
        
    }
    
    public String modify(){
        System.out.println("sto modificando l'evento");
        em.modify(eventViewed, invited, oldInvited);
        invited.clear();
        return "eventPage?faces-redirect=true&idEv="+eventViewedId;
    }
    
    public void loadEvent(){
        invited.clear();
        eventViewed = em.findEvent(eventViewedId);
        for(Participant p : eventViewed.getParticipantCollection()){
            invited.add(p.getUser1().getEmail());
        }
        oldInvited = invited.size();
        System.out.println(eventViewed.getId());
    }
    
    public boolean checkCreator(){
        return !(em.checkCreator(eventViewed));
    }
    
    public boolean checkPaticipant(){
        return !(em.checkParticipant(eventViewed));
    }
    
    public String invitings(String backPage){
        return "invitings?faces-redirect=true&page="+backPage;
    }
    
    public String next(){
        return "eventPage";
    }
    
    public String back(){
        return "newEventPage";
    }
    
    
    

    
}
